﻿using BGYXControlLibrary.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace BGYXControlLibrary
{
    public class BGYX_Slider : Slider
    {
        private Popup TipPopup = null;
        private MethodInfo PopupUpdatePositionMethod = null;

        static BGYX_Slider()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BGYX_Slider), new FrameworkPropertyMetadata(typeof(BGYX_Slider)));
        }

        public BGYX_Slider()
        {
            this.ValueChanged += BGYX_Slider_ValueChanged;
        }

        private void BGYX_Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (TipPopup == null)
            {
                TipPopup = BGYX_VisualTreeHelper.FindVisualChild<Popup>(this, "TipPopup");
            }
            if (TipPopup != null && TipPopup.IsOpen)
            {
                if (PopupUpdatePositionMethod == null)
                {
                    PopupUpdatePositionMethod = typeof(Popup).GetMethod("UpdatePosition", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                }
                if (PopupUpdatePositionMethod != null)
                {
                    PopupUpdatePositionMethod.Invoke(TipPopup, null);
                }
            }
        }
    }
}
